﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace StateManager
{
    class SinglePlayerLevel : Level
    {
        
   
        LZone[] landingZone;
        const int LZONES = 5;
        
        

        public SinglePlayerLevel(IServiceProvider serviceProvider, Stream fileStream, SpriteBatch spriteBatch,
            int x, int y, int lnumber,ref int score,ref int lives)
            : base(serviceProvider, fileStream, spriteBatch, x, y, lnumber)
        {
            player = new Player(score, lives,this);
            landingZone = new LZone[LZONES];

            float thing = 1280 / 5;

            for (int i = 0; i < LZONES; i++)
                landingZone[i] = new LZone((int)(thing * (i + 1) - 80), this);

        }


        public override void updateLevel(KeyboardState kps, GamePadState gps, GameTime gameTime, ref int Score, ref int lives, ref bool allPlacesLanded)
        {
 
            lives = player.Lives;
            Score = player.Score;
            checkLocation chk = checkLocation.onGround;
            checkLocation test;

            bool isCrocDead = false;
            bool isLog = false;
            float vel = 0;
            Direction dir = Direction.Left;
            player.Update(kps,gps, gameTime);

            if (snake != null)
            {
                snake.Update(gameTime, viewPort);
                
            }
            for (int x = 0; x < MAXLANES; x++)
            {
                for (int y = 0; y < MAXCARS; y++)
                {
                    logs[x, y].Update(gameTime, viewPort);
                    cars[x, y].Update(gameTime, viewPort);
                    if (player.IsAlive)
                    {
                        if (snake != null)
                            handleCollisions(snake, player);

                        handleCollisions(cars[x, y], player);


                        test = logs[x, y].HandleCollisions(this, player);
                        if (test != checkLocation.onGround)
                        {

                            if (!isLog && test != checkLocation.onLog && !isCrocDead && test != checkLocation.crocbite)
                                chk = test;
                            else if (test == checkLocation.onLog || test == checkLocation.crocbite || test == checkLocation.turtledrown)
                            {
                               


                                if (test == checkLocation.onLog)
                                    isLog = true;

                                if (test == checkLocation.crocbite)
                                    isCrocDead = true;

                                 if (!isLog || !isCrocDead)
                                    chk = test;
                                
                                if (test == checkLocation.onLog)
                                {
                                    vel = logs[x, y].Vel.X;
                                    dir = logs[x, y].dir;
                                }
                            }

                        }
                    }

                }

            }
            for (int i = 0; i < LZONES; i++)
            {
                if (landingZone[i].colRec.Intersects(player.plyrRect) && !player.HasWon)
                {

                    landingZone[i].landed();
                    playSound(4);
                    landingZone[i].ColorLanded = player.FrogColor;
                    allZonesFilled++;
                    if (allZonesFilled == LZONES)
                    {
                        player.playerWin();
                        allPlacesLanded = true;
                    }
                    else
                        player.respawnPlayer();
                }
            }
            switch (chk)
            {
                case checkLocation.drowning:
                    player.killPlayer('d');
                    if (!player.HasWon)
                        playSound(3);
                    break;
                case checkLocation.turtledrown:
                    player.killPlayer('d');
                    if (!player.HasWon)
                        playSound(3);
                    break;
                case checkLocation.crocbite:
                    player.killPlayer('c');
                    playSound(2);
                    break;
                case checkLocation.onLog:
                    player.movePlayer(vel, dir);
                    break;
                case checkLocation.onGround:
                    break;
                default:
                    break;
            }
            


        }

        public override void Draw(GameTime gameTime, SpriteBatch sb)
        {
            base.Draw(gameTime, sb);
            foreach (LZone lzone in landingZone)
            {
                lzone.Draw(sb, gameTime);
            }
            sb.DrawString(menuFont, "Score: " + player.Score, new Vector2(0, 50), Color.White);
            sb.DrawString(menuFont, "Lives: " + player.Lives, new Vector2(0, 10), Color.White);
        }
    }
}
